package com.cn.bulu.utils.upload;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import java.io.IOException;
//七牛上传图片的工具类
public class UploadDemo {
	  //设置好账号的ACCESS_KEY和SECRET_KEY
//	  String ACCESS_KEY = "7iWH7YPKy66hAhyR-RbUTqHbFw1SFARjRBHU3BQr";
//	  String SECRET_KEY = "o_vDvsVLoFm9Pxd-FqmOK-0tfCHgOSv9_9-oet6d";
	  String ACCESS_KEY = "dOcl9dBDG3slxqYH0ISdXwv6slScN3ovAHrAocgm";
	  String SECRET_KEY = "WiWNmd-n4aT-OTkowxenXes6ETul7JcvUo4-dGr8";
	  //要上传的空间
	  String bucketname = "test";
	  //密钥配置
	  Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
	  //创建上传对象
	  UploadManager uploadManager = new UploadManager();
	  //简单上传，使用默认策略，只需要设置上传的空间名就可以了
	  public String getUpToken(){
	      return auth.uploadToken(bucketname);
	  }

	  public int upload(String FilePath,String key) throws IOException{
		Response res =null;
	    try {
	      //调用put方法上传
	      res = uploadManager.put(FilePath, key, getUpToken());
	      //打印返回的信息
	      } catch (QiniuException e) {
	          Response r = e.response;
	          // 请求失败时打印的异常的信息
	          System.out.println(r.toString());
	          try {
	              //响应的文本信息
	            System.out.println(r.bodyString());
	          } catch (QiniuException e1) {
	              //ignore
	          }
	      }
	    return res.statusCode;
	  }
}